/*
 * 文 件 名 : SmileyParser.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-20
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fire8.dolphin.ui.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

/**
 * 文字与表情转换操作
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-8-20]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class SmileyParser
{
    private static SmileyParser sInstance = null;
    
    private Context mContext = null;
    
    private Pattern mPattern = null;
    
    private HashMap<String, Integer> mSmileyTextToId = null;
    
    private final String[] mSmileyArrays = {"/呲牙", "0", "/调皮", "1", "/流汗", "2", "/偷笑", "3", "/再見", "4",
                                            "/敲打", "5","/擦汗", "6","/猪头","7","/玫瑰", "8","/流泪", "9",
                                            "/大哭", "10","/嘘", "11","/酷", "12", "/抓狂", "13","/委屈", "14", 
                                            "/大便","15", "/炸弹", "16", "/菜刀", "17", "/可愛", "18",  "/色", "19",
                                            "/害羞", "20", "/得意", "21","/吐", "22","/微笑", "23","/发怒", "24", 
                                            "/尴尬", "25","/惊恐", "26","/冷汗", "27", "/爱心", "28",
                                            "/白眼","30", "/傲慢", "31", "/难过","32","/惊讶", "33", "/疑问", "34", 
                                            "/睡觉","35", "/憨笑", "37", "/衰","39", "/撇嘴", "40", "/阴险", "41", 
                                            "/奋斗","42", "/发呆","43","/右哼哼", "44", "/拥抱", "45", "/坏笑","46", 
                                            "/鄙视","48", "/晕","49", "/大兵", "50", "/可怜", "51", "/强", "52",
                                            "/弱", "53", "/握手", "54", "/胜利", "55", "/抱拳", "56", "/凋谢", "57",  
                                            "/米饭", "58", "/蛋糕","59", "/西瓜","60", "/啤酒","61","/瓢虫", "62", 
                                            "/OK", "64", "/爱你", "65", "/咖啡", "66", "/钱", "67",            
                                            "/月亮", "68", "/美女", "69", "/刀", "70", "/差劲", "72", "/拳头", "73",
                                            "/心碎", "74", "/太阳", "75", "/礼物", "76", "/足球", "77", 
                                            "/骷髅", "78", "/闪电", "80", "/饥饿", "81", "/困", "82", "/咒骂", "83", 
                                            "/折磨", "84", "/抠鼻", "85", "/鼓掌", "86", "/糗大了", "87",
                                            "/左哼哼", "88", "/打哈欠", "89", "/快哭了", "90", "/吓", "91", 
                                            "/篮球", "92", "/乒乓", "93", "/NO", "94", "/闭嘴", "106"};
    
    private int[] mSmileyIds = null;
    
    private String[] mSmileyTexts = null;
    
    public static SmileyParser getInstance(Context context)
    {
        if (sInstance == null)
        {
            sInstance = new SmileyParser(context);
            
        }
        
        return sInstance;
    }
    
    private SmileyParser(Context context)
    {
        // TODO Auto-generated constructor stub
        mContext = context;
        initSmileyIds();
        mPattern = buildPattern();
        mSmileyTextToId = buildSmileyRes();
    }
    
    private void initSmileyIds()
    {
        mSmileyIds = new int[mSmileyArrays.length / 2];
        mSmileyTexts = new String[mSmileyArrays.length / 2];
        for (int i = 0; i < mSmileyArrays.length / 2; i++)
        {
            mSmileyTexts[i] = mSmileyArrays[i * 2];
            mSmileyIds[i] = Integer.parseInt(mSmileyArrays[i * 2 + 1]);
        }
    }
    
    public int[] getSmileyIDs()
    {
        return mSmileyIds;
    }
    
    public int getSmileyResourceId(int smileyId)
    {
        String idString ="f"+ String.format("%03d",smileyId); 
        int id = getResId(idString, mContext, R.drawable.class);
        return id;
    }
    
    public static int getResId(String variableName, Context context, Class<?> c)
    {
        
        try
        {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }
    
    public String[] getSmileyTexts()
    {
        return mSmileyTexts;
    }
    
    Drawable getSmileyDrawable(int id)
    {
        Drawable drawable = null;
        drawable = mContext.getResources().getDrawable(getSmileyResourceId(id));
        
        return drawable;
        
    }
    
    /**
    * 建立String - Id的对应关系
    */
    private HashMap<String, Integer> buildSmileyRes()
    {
        
        HashMap<String, Integer> smileyTextToId = new HashMap<String, Integer>(
                mSmileyIds.length);
        for (int i = 0; i < mSmileyIds.length; ++i)
        {
            smileyTextToId.put(mSmileyTexts[i], mSmileyIds[i]);
        }
        
        return smileyTextToId;
    }
    
    /**
    * 建立匹配用的正则表达式
    * @return
    */
    private Pattern buildPattern()
    {
        StringBuilder builder = new StringBuilder(mSmileyTexts.length * 3);
        builder.append('(');
        for (String s : mSmileyTexts)
        {
            builder.append(Pattern.quote(s));
            builder.append('|');
        }
        
        builder.replace(builder.length() - 1, builder.length(), ")");
        
        return Pattern.compile(builder.toString());
    }
    
    /**
    * 把文字转换为图片
    * @param text
    * @return
    */
    public Spannable addSmileySpans(CharSequence text)
    {
        if(text == null)
        {
            return null;
        }
        SpannableStringBuilder spBuilder = new SpannableStringBuilder(text);
        
        Matcher matcher = mPattern.matcher(text);
        
        while (matcher.find())
        {
            int id = mSmileyTextToId.get(matcher.group());
            //matcher.start(),matcher.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spBuilder.setSpan(new ImageSpan(mContext, getSmileyResourceId(id),
                    ImageSpan.ALIGN_BASELINE),
                    matcher.start(),
                    matcher.end(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            
        }
        
        return spBuilder;
    }
}
